package mapper

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"ksd-social-api/modules/note/model"
)

type NoteLessonMapper struct {
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询小册信息
 */
func (mapper *NoteLessonMapper) FindLessonsStatus(noteId uint64, chapterId uint64, systemId uint64) ([]*model.NoteLesson, error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 3: 读取id的数据放入到结构体，进行返回
	// 1：准备容器对象，开始装载数据库数据
	noteLessonList := []*model.NoteLesson{}
	_, err := db.Raw(`select 
			id,
			note_id,
			chapter_id,
			title,
			sorted,
			playcount,
			videoid,
			duration,
			videosize,
			video_source_url,
			free_flag,
			create_time,
			update_time,
			chapter_title,
			note_title,
			status,
			system_id,
			system_name
    from xk_note_lesson where note_id = ? and chapter_id = ?  and system_id = ?  and  status = 1 and is_deleted = 0 order by sorted asc`).SetArgs(noteId, chapterId, systemId).QueryRows(&noteLessonList)
	// 4: 结构返回
	return noteLessonList, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询小册信息
 */
func (mapper *NoteLessonMapper) FindLessons(noteId uint64, chapterId uint64, systemId uint64) ([]*model.NoteLesson, error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 3: 读取id的数据放入到结构体，进行返回
	// 1：准备容器对象，开始装载数据库数据
	noteLessonList := []*model.NoteLesson{}
	_, err := db.Raw(`select 
			id,
			note_id,
			chapter_id,
			title,
			sorted,
			playcount,
			videoid,
			duration,
			videosize,
			free_flag,
			create_time,
			update_time,
			video_source_url,
			chapter_title,
			note_title,
			system_id,
			status,
			system_name
    from xk_note_lesson where note_id = ? and chapter_id = ?  and system_id = ? order by sorted asc`).SetArgs(noteId, chapterId, systemId).QueryRows(&noteLessonList)
	// 4: 结构返回
	return noteLessonList, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询小册信息
 */
func (mapper *NoteLessonMapper) RemoveById(id uint64, noteId uint64, systemId uint64) (flag bool, err error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	dbLesson := model.NoteLesson{Id: id, NoteId: noteId, SystemId: systemId}
	// 3: 读取id的数据放入到结构体，进行返回
	i, err := db.Delete(&dbLesson)
	// 4: 结构返回
	return i > 0, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询小册信息
 */
func (mapper *NoteLessonMapper) GetById(id uint64, systemId uint64) (noteLesson *model.NoteLesson, err error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	dbLesson := model.NoteLesson{Id: id, SystemId: systemId}
	// 3: 读取id的数据放入到结构体，进行返回
	err = db.Read(&dbLesson, "id", "system_id")
	// 4: 结构返回
	return &dbLesson, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 保存小册章信息
 */
func (mapper *NoteLessonMapper) SaveLesson(noteLesson *model.NoteLesson) (lesson *model.NoteLesson, err error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 3: 读取id的数据放入到结构体，进行返回
	_, err = db.Insert(noteLesson)
	// 4: 结构返回
	return noteLesson, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id修改小册信息
 */
func (mapper *NoteLessonMapper) UpdateById(field string, value any, id uint64) (flag bool, err error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 3: 读取id的数据放入到结构体，进行返回
	exec, err := db.Raw(fmt.Sprintf("update xk_note_lesson set %s=?,update_time = now() where id = ?", field)).SetArgs(value, id).Exec()
	// 4: 结构返回
	affected, _ := exec.RowsAffected()
	return affected > 0, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id修改小册信息
 */
func (mapper *NoteLessonMapper) UpdateByIdContent(mdcontent string, htmlcontent string,
	systemId uint64, noteId uint64, id uint64) (flag bool, err error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 3: 读取id的数据放入到结构体，进行返回
	exec, err := db.Raw("update xk_note_lesson set mdcontent=?,htmlcontent=?,update_time = now() where id = ? and note_id = ? and system_id = ?").SetArgs(mdcontent, htmlcontent, id, noteId, systemId).Exec()
	db.Raw("update xk_note set update_time = now() where id = ? and system_id = ?").SetArgs(noteId, systemId).Exec()
	// 4: 结构返回
	affected, _ := exec.RowsAffected()
	return affected > 0, err
}
